package cn.b.sky.mes.mm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import cn.b.sky.mes.mm.dao.WarehouseDao;
import cn.b.sky.mes.mm.model.Warehouse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.mm.model.StorageLocationType;
import cn.b.sky.mes.mm.model.condition.WarehouseCondition;
import cn.b.sky.mes.mm.service.WarehouseService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

@Service("warehouseService")
public class WarehouseServiceImpl implements WarehouseService {

  private static final Logger LOG = LoggerFactory.getLogger(WarehouseServiceImpl.class);

  @Resource
  WarehouseDao warehouseDao;

  @Override
  public List<Warehouse> selectWarehouseListByCondition(WarehouseCondition condition) {
    if (StringUtils.isBlank(condition.getOrderField())) {
      condition.setOrderField("CreateDateTime");
    }
    return warehouseDao.selectWarehouseListByCondition(condition);
  }

  @Override
  public int selectWarehouseCountByCondition(WarehouseCondition condition) {
    return warehouseDao.selectWarehouseCountByCondition(condition);
  }

  @Override
  public void insertWarehouse(Warehouse warehouse) {
    if (StringUtils.isBlank(warehouse.getWarehouseCode()) || StringUtils.isBlank(warehouse.getSiteCode())) {
      return;
    }
    warehouseDao.insertWarehouse(warehouse);
  }

  @Override
  public void updateWarehouse(Warehouse warehouse) {
    if (StringUtils.isBlank(warehouse.getWarehouseCode()) || StringUtils.isBlank(warehouse.getSiteCode())) {
      return;
    }
    warehouseDao.updateWarehouse(warehouse);

  }

  @Override
  public BatchJobReport deleteWarehouse(String warehouseCode, String siteCode) {
    String languageCode = SecurityContextUtil.getLanguageCode();
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(warehouseCode)) {
      return new BatchJobReport("", "siteCode or warehouseCode is null", false, languageCode, SecurityContextUtil.getSiteCode());
    }
    String message = warehouseCode;
    boolean success = true;
    String cause = "";

    try {
      warehouseDao.deleteWarehouse(warehouseCode, siteCode);
    } catch (Exception e) {
      LOG.error("{}", e);
      success = false;
      String errorMsg = e.getMessage();
      if (DatabaseUtils.isConstraintsError(e.getMessage())) {
        //外键删除已存在提示
        errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),languageCode, "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),languageCode, "notDelete.M");
      }
      cause = errorMsg;
    }
    return new BatchJobReport(message, cause, success, languageCode, SecurityContextUtil.getSiteCode());
  }

  @Override
  public Warehouse getWarehouse(String warehouseCode, String siteCode) {
    if (StringUtils.isBlank(warehouseCode) || StringUtils.isBlank(siteCode)) {
      return null;
    }
    return warehouseDao.getWarehouse(warehouseCode, siteCode);
  }

  @Override
  public List<StorageLocationType> selectStorageLocationListByWarehouseCode(String id, String siteCode) {
    return warehouseDao.selectStorageLocationListByWarehouseCode(id, siteCode);
  }

  @Override
  public List<Warehouse> selectWarehouseListBySiteCode(String siteCode) {
    if (StringUtils.isBlank(siteCode)) {
      return null;
    }
    return warehouseDao.selectWarehouseListBySiteCode(siteCode);
  }

  @Override
  public List<Warehouse> selectWarehouseListSuggests(WarehouseCondition condition) {
    return warehouseDao.selectWarehouseListSuggests(condition);
  }

  @Override
  public List<Warehouse> selectUnLockWarehouseListBySiteCode(String siteCode) {
    if (StringUtils.isBlank(siteCode)) {
      return null;
    }
    return warehouseDao.selectUnLockWarehouseListBySiteCode(siteCode);
  }

}
